package avicit.bdp.dcs.threshold.other;

import avicit.bdp.dcs.threshold.utils.TestUtils;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;

import static org.junit.Assert.assertEquals;

public class FileTest {
    private RestTemplate restTemplate = new RestTemplate();

    private static String fileName = "file_" + System.currentTimeMillis() + ".csv";
    private static String catalogName = "catalog_" + System.currentTimeMillis();

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        System.out.println("用例初始化...");
        TestUtils.createFile(fileName);
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        System.out.println("用例执行完成，开始清理...");
        TestUtils.deleteFile(fileName);
    }

    /***********************************************Begin 测试用例 ***********************************************/
    @Test(timeout = 5 * 60 * 1000)
    public void testFile() throws Exception {
        // 创建目录
        JSONObject param0 = new JSONObject();
        param0.put("parentCatalogPath", "/dcs");
        param0.put("catalogName", catalogName);
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/saveCatalog",
                TestUtils.buildPostHttpEntity(param0.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 删除目录
        JSONObject param01 = new JSONObject();
        param01.put("catalogPath", "/dcs/" + catalogName);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/deleteCatalogByPath",
                HttpMethod.DELETE, TestUtils.buildDeleteHttpEntity(param01.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 查询文件列表
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/getFileList?catalogPath=/dcs&sort=1",
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 上传文件
        File file = new File(System.getProperty("user.dir") + "/" + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(System.getProperty("user.dir") + "/" + fileName);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();

        form.add("allowResume", true);
        form.add("fileName", fileName);
        form.add("fileMd5", TestUtils.getMD5(file));
        form.add("chunk", 1);
        form.add("chunks", 1);
        form.add("chunkSize", file.length());
        form.add("fileSize", file.length());
        form.add("hdfsPath", "/dcs");
        form.add("chunkSize", 1);
        form.add("file", fileSystemResource);

        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/upload",
                TestUtils.buildUploadFileHttpEntity(form), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 合并文件
        JSONObject json = new JSONObject();
        json.put("fileName", fileName);
        json.put("fileSize", file.length());
        json.put("hdfsPath", "/dcs");

        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/mergeChunks",
                TestUtils.buildPostHttpEntity(json.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 生成模板
        JSONObject param = new JSONObject();
        param.put("catalogPath", "/dcs");
        param.put("filePath", fileName);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/generateTemplate",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 取消关联模板
        JSONObject param1 = new JSONObject();
        param1.put("filePath", "/dcs/" + fileName);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/cancelFileTemplate",
                TestUtils.buildPostHttpEntity(param1.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 删除文件
        JSONObject param2 = new JSONObject();
        param2.put("filePath", "/dcs/" + fileName);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/file/FileRest/deleteFileByPaths",
                HttpMethod.DELETE, TestUtils.buildDeleteHttpEntity(param2.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
